package com.zrx.springboot.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrx.springboot.entity.Book;
import com.zrx.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;


@Controller

public class BcakBookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/editable_table")
    public String mainPage(HttpSession session) {

        return "form/editable_table";

    }
    /**
     * 获取图书信息
     */
    @RequestMapping("/getBackBookData")
    public String getBookData(Model model, Integer page, Integer category,Integer size){
        System.out.println("显示图书信息------------");
        //mybatis puls分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        IPage<Book> iPage = bookService.page(new Page<>(page,size),queryWrapper);
//        model.addAttribute("book", iPage.getCurrent());
        model.addAttribute("iPage", iPage);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("last", iPage.getPages());//总共页
        model.addAttribute("category",category);
        model.addAttribute("size",size);
        return "form/bookDate";
    }
    /**
     * 根据名字模糊查询获取图书信息
     */
    @RequestMapping("/getBackBookNameData")
    public String getBookNameData(Model model, Integer page,Integer size,String bookname){

        System.out.println("显示图书信息------------");
        System.out.println(page);
        System.out.println(size);
        System.out.println(bookname);
        //mybatis puls分页

        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",bookname);
        IPage<Book> iPage = bookService.page(new Page<>(page,size),queryWrapper);

//        model.addAttribute("book", iPage.getCurrent());
        model.addAttribute("iPage", iPage);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("last", iPage.getPages());//总共页
        model.addAttribute("size",size);
        return "form/bookDate";

    }

    /**
     *根据id查找图书
     */
    @RequestMapping("/getBookByID")
    public String getBookByID(Integer id,Model model){
        Book book = bookService.getById(id);
        model.addAttribute("book",book);


        return "form/editable_table_book";
    }
    /**
     *根据id删除图书
     */
    @RequestMapping("/deletebook")
    public String deletebook(Integer id){
        System.out.println(id);
        bookService.removeById(id);



        return "form/editable_table";
    }


    /**
     *addbook页面
     */
    @RequestMapping("/editable_table_addbook")
    public String editable_table_addbook(HttpSession session) {

        return "form/editable_table_addbook";

    }

    /**
     *添加图书信息
     */
    @RequestMapping("/addbook")
    public String savebook(HttpServletRequest request,Book book , MultipartFile upload) throws IOException {
        System.out.println("--------------------");
        java.util.Date date = new java.util.Date();

        Timestamp t= new Timestamp(date.getTime());
        System.out.println(t);
        System.out.println(book);

        book.setPublishDate(t);

        //上传文件的名称
        String filename = upload.getOriginalFilename();
        System.out.println(filename);
        //上传的位置

        String uploadDir=ResourceUtils.getURL("classpath:").getPath()+"static/images/book/";
        System.out.println(uploadDir);
        //如果目录不存在，自动创建文件夹
        File dir = new File(uploadDir);
        if(!dir.exists())
        {
            dir.mkdir();
            System.out.println("path:" + uploadDir);

        }
        //完成文件上传
        upload.transferTo(new File(uploadDir, filename));


        book.setImgUrl(filename);
        System.out.println(book);
        bookService.save(book);

        return "form/editable_table";
    }
    /**
     * 修改图书信息
     */
    @RequestMapping("/upbook")
    public String upbook(HttpServletRequest request,Book book , MultipartFile upload) throws IOException {
        System.out.println("---------修改---------");
        java.util.Date date = new java.util.Date();

        Timestamp t= new Timestamp(date.getTime());
        System.out.println(t);
        book.setPublishDate(t);
        System.out.println(book);




            //上传文件的名称
        System.out.println("upload:"+upload);
        System.out.println("------------");
        System.out.println("name:"+upload.getName());
            String filename = upload.getOriginalFilename();
            System.out.println("filename"+filename);
            //上传的位置
            if (filename.equals("")){
                Book book1 = bookService.getById(book.getId());
                book.setImgUrl(book1.getImgUrl());
                bookService.updateById(book);
            }else {
                String uploadDir = ResourceUtils.getURL("classpath:").getPath() + "static/images/book/";
                System.out.println("uploadDir："+ uploadDir);
                //如果目录不存在，自动创建文件夹
                File dir = new File(uploadDir);
                if (!dir.exists()) {
                    dir.mkdir();
                    System.out.println("path:" + uploadDir);
                }
                //完成文件上传
                upload.transferTo(new File(uploadDir, filename));
                book.setImgUrl(filename);
                bookService.updateById(book);
            }


        System.out.println(book);


        return "form/editable_table";
    }

}
